--
-- Author: yawei.zhu
-- Date: 2018-07-13 15:02:41
--
-- PvpChampionFormRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode( Res.ChampionFormTeamNode ))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._ui.Panel:addClickEventListener(function()

    end)

    self:refresh()
end

-- bg 显示顺序  空、冠军、自己、失败、其他
function ClassRef:refresh()
    self:clear()
    if not self._data then 
        self._ui.bg_empty:setVisible(true)
        return 
    end

    local subName = string.usubstr(self._data.teamName, 6)
    if string.ulen(self._data.teamName) > 6 then
        subName = subName .. "..."
    end
    self._ui.name:setString(subName)

    -- 是否失败的判断（当前节点队伍的轮数 和 相邻的节点队伍的轮数 进行比较）
    local anotherRound = 0
    local anotherIdx = self._index - 1
    if self._index % 2 == 1 then
        anotherIdx = self._index + 1
    end
    local anotherInfo = self._parent:getTeamInfo(anotherIdx)
    if anotherInfo then anotherRound = anotherInfo.round end

    if self._myTeamId == self._data.teamId then
        self._ui.bg_my:setVisible(true)
        self._ui.icon_my:setVisible(true)
    elseif anotherRound > self._data.round then
        self._ui.bg_fail:setVisible(true)
    else
        self._ui.bg:setVisible(true)
    end
end

function ClassRef:setNeedInfo(idx, parent, myTeamId)
    self._index = idx
    self._parent = parent
    self._myTeamId = myTeamId
end

-- 设置冠军标记
function ClassRef:setChampionFlag()
    if not self._data then return end
    self:clear()
    self._ui.bg_first:setVisible(true)
    self._ui["Panel_play/bg_1"]:setVisible(true)
    self._ui["Panel_play/bg"]:setVisible(false)
    
    local subName = string.usubstr(self._data.teamName, 6)
    if string.ulen(self._data.teamName) > 6 then
        subName = subName .. "..."
    end
    self._ui.name:setString(subName)
    self._ui.name:setTextColor(cc.c4b(255, 239, 166, 0xFF))
end

function ClassRef:setScore(scoreL, scoreR)
    self._ui.Panel_play:setVisible(true)
    self._ui["Panel_play/time"]:setString(string.format("%d:%d", scoreL, scoreR))
end

function ClassRef:clear()
    self._ui.bg_choose:setVisible(false)
    self._ui.bg_my:setVisible(false)
    self._ui.bg_fail:setVisible(false)
    self._ui.bg_empty:setVisible(false)
    self._ui.bg_first:setVisible(false)
    self._ui.bg:setVisible(false)
    self._ui.icon_my:setVisible(false)
    self._ui.name:setString("")
    self._ui.name:setTextColor(cc.c4b(0x2F, 0x0C, 0x04, 0xFF))
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
